﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface ICKLogService : IAppService
    {
        List<CKLog> GetList(string CateID);
         
        void Add(AddCKLogInput input);
        void Update(UpdateCKLogInput input);
        void Delete(string id );

        void UpdateNum(string Id, int Num);

        CKLog GetModel(string Code); 

        PagedData<CKLog> GetPageData(Pagination page, string Code, string CateID);
    }

    public class CKLogService : AppServiceBase<CKLog>, ICKLogService
    {
        public CKLogService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<CKLog> GetList(string CateID)
        {
            var q = this.Query;
            
            if (CateID.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.CateID.Contains(CateID));
            }

            var ret = q.ToList();
            return ret;
        }

        

        public void Add(AddCKLogInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateCKLogInput input)
        {
            this.UpdateFromDto(input);
        }

        public CKLog GetModel(string Code)
        {
            return  this.Query.Where(a => a.Code == Code).FirstOrDefault();
        }

        public void Delete(string id)
        {
            this.DbContext.Delete<CKLog>(a => a.Id == id);
        }

        public void UpdateNum(string Id,int Num)
        {
            this.DbContext.Update<CKLog>(a => a.Id == Id, a => new CKLog()
            {
                Num = Num
            });
        }


        public PagedData<CKLog> GetPageData(Pagination page, string Code, string CateID)
        {
            var q = this.DbContext.Query<CKLog>();            
            q = q.WhereIfNotNullOrEmpty(Code, a => a.Code==Code);
            q = q.WhereIfNotNullOrEmpty(CateID, a => a.CateID == CateID);
            q = q.OrderByDesc(a => a.CreateDate);
            PagedData<CKLog> pagedData = q.TakePageData(page);
            return pagedData;
        }

 


    }

}
